package com.simple.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.simple.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.simple.common.annotation.Log;
import com.simple.common.core.controller.BaseController;
import com.simple.common.core.domain.AjaxResult;
import com.simple.common.enums.BusinessType;
import com.simple.project.domain.Category;
import com.simple.project.service.ICategoryService;
import com.simple.common.utils.poi.ExcelUtil;

/**
 * 博客分类，二级分类Controller
 * 
 * @author simple
 * @date 2020-12-01
 */
@RestController
@RequestMapping("/project/category" )
public class CategoryController extends BaseController {

    @Autowired
    private ICategoryService iCategoryService;

    /**
     * 查询博客分类列表
     */
    @PreAuthorize("@ss.hasPermi('project:category:list')")
    @GetMapping("/list")
    public AjaxResult list(Category category) {
        LambdaQueryWrapper<Category> lqw = Wrappers.lambdaQuery(category);
        List<Category> list = iCategoryService.list(lqw);
        return AjaxResult.success(list);
    }

    /**
     * 导出博客分类列表
     */
    @PreAuthorize("@ss.hasPermi('project:category:export')" )
    @Log(title = "博客分类，二级分类" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Category category) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<Category>(category);
        List<Category> list = iCategoryService.list(lqw);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category. class);
        return util.exportExcel(list, "category" );
    }

    /**
     * 获取博客分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:category:query')" )
    @GetMapping(value = "/{categoryId}" )
    public AjaxResult getInfo(@PathVariable("categoryId" ) Long categoryId) {
        return AjaxResult.success(iCategoryService.getById(categoryId));
    }

    /**
     * 新增博客分类
     */
    @PreAuthorize("@ss.hasPermi('project:category:add')" )
    @Log(title = "博客分类" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Category category) {
        return toAjax(iCategoryService.save(category) ? 1 : 0);
    }

    /**
     * 修改博客分类
     */
    @PreAuthorize("@ss.hasPermi('project:category:edit')" )
    @Log(title = "博客分类" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Category category) {
        return toAjax(iCategoryService.updateById(category) ? 1 : 0);
    }

    /**
     * 删除博客分类
     */
    @PreAuthorize("@ss.hasPermi('project:category:remove')" )
    @Log(title = "博客分类" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}" )
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(iCategoryService.removeByIds(Arrays.asList(categoryIds)) ? 1 : 0);
    }
}
